package thread;

/**
 * 线程不安全
 */
public class NoSynchronizedTest  extends Thread{

    public static void main(String[] args) throws InterruptedException {
        NoSynchronizedTest noSynchronizedTest = new NoSynchronizedTest();
        NoSynchronizedTest noSynchronizedTest1 = new NoSynchronizedTest();
        noSynchronizedTest.start();
        noSynchronizedTest1.start();
        noSynchronizedTest.join();
        noSynchronizedTest1.join();
        System.out.println(a);
    }
    static  int a = 0;

    /**
     * synchronized 作用于普通方法，锁的对象是当前实例
     * synchronized 作用于静态方法，锁的对象是类的Class对象
     * synchronized 作用于方法块，锁的对象是括号里匹配的对象
     */
    public synchronized void add(){
        for (int i = 0;i<10000;i++){
            a++;
        }
    }
    @Override
    public void run() {
        add();
    }
}
